<template>
  <PageTemplate ref="pageTemplate"
                :tableRequest="tableRequest"
                rowKey="affairId"
                :metaDataRequestParams="metaDataRequestParams"
                :toolButtonList="toolButtonList"
                :tableSlotNames="tableSlotNames"
                @onToolClick="handleToolClick"
                @onRowClick="onRowClick"
                @onMetaDataChange="metaDataChangeHandle">
    <!-- 标题 -->
    <span slot="subject"
          slot-scope="{ text, record }">
      <SubjectTableRender :slotData="record"></SubjectTableRender>
    </span>
    <!-- 流程期限 -->
    <span slot="processDeadLineName"
          slot-scope="{ text, record }">
      <ProcessDeadLineNameTableRender :slotData="record"></ProcessDeadLineNameTableRender>
    </span>
    <!-- 是否归档 -->
    <span slot="affairArchiveId"
          slot-scope="{ text, record }">
      <AffairArchiveIdTableRender :slotData="record"></AffairArchiveIdTableRender>
    </span>
    <!-- 跟踪状态 -->
    <span slot="isTrack"
          slot-scope="{ text, record }">
      <IsTrackTableRender :dataType="dataType"
                          :slotData="record"></IsTrackTableRender>
    </span>
    <!--  -->
    <span slot="processId"
          slot-scope="{ text, record }">
      <ProcessIdTableRender :dataType="dataType"
                            :slotData="record"></ProcessIdTableRender>
    </span>
    <!-- 当前待办人 -->
    <span slot="currentNodesInfo"
          slot-scope="{ text, record }">
      <CurrentNodesInfoTableRender :dataType="dataType"
                                   :slotData="record">
      </CurrentNodesInfoTableRender>
    </span>
  </PageTemplate>
</template>
<script>
  import * as Fiber from 'fiber';
  import PageTemplate from '../../../components/page-template';
  import { getParams } from '../../../utils';
  import toolbarMixins from '../mixins/toolbarMixins';
  import smartCodeMixin from '../../../mixins/smart-code-mixin';
  import { showDetailPage } from '../../../utils/index';
  import { isAffairValid } from '../collaborationFunc/collaboration';
  // 表格slot组件
  import SubjectTableRender from '../components/subject-table-render.vue';
  import ProcessDeadLineNameTableRender from '../components/processDeadLineName-table-render.vue';
  import AffairArchiveIdTableRender from '../components/affairArchiveId-table-render.vue';
  import IsTrackTableRender from '../components/isTrack-table-render.vue';
  import ProcessIdTableRender from '../components/processId-table-render.vue';
  import CurrentNodesInfoTableRender from '../components/currentNodesInfo-table-render.vue';
  const urlParamObj = getParams(window.location.search);
  export default {
    name: 'SentListMain',
    mixins: [toolbarMixins, smartCodeMixin],
    components: {
      PageTemplate,
      SubjectTableRender,
      ProcessDeadLineNameTableRender,
      AffairArchiveIdTableRender,
      IsTrackTableRender,
      ProcessIdTableRender,
      CurrentNodesInfoTableRender
    },
    data() {
      return {
        allToolBtns: [],
        isSecretLevelEnable: false, // 需要后端数据  ${ctp:isSecretLevelEnable()};
        hasDoc: false, // 需要后端数据  "${ctp:hasPlugin('doc')}";
        haveNewColl: false, // 需要后端数据
        newColNodePolicy: {
          relateAttachment: false,
          editWorkFlow: true,
          repeatSend: true,
          cancel: true,
          reMove: false,
          uploadAttachment: true,
          uploadRelDoc: true,
          print: true,
          pigeonhole: true,
          forward: true
        }, // 需要后端数据  {"newColNodePolicy":'${newColnewColNodePolicy}'};
        resources: null, // 需要后端数据
        emailShow: true, // 需要后端数据  ${v3x:hasPlugin('webmail')};
        isV5Member: true, // 需要后端数据  ${CurrentUser.externalType == 0};
        isFormAdvanced: true, // 需要后端数据  "${ctp:hasPlugin('formAdvanced')}";
        hasDumpData: true, // 需要后端数据  "${ctp:escapeJavascript(hasDumpData)}";
        showHandoverButton: true, // 需要后端数据  "${ctp:escapeJavascript(showHandoverButton)}";
        addinMenus: true, // 需要后端数据 var addinMenus = new Array();
        // <c:forEach var="addinMenu" items="${AddinMenus}" varStatus="status">
        //     var index = ${status.index};
        //     addinMenus[index] = {pluginId : '${addinMenu.pluginId}',name : '${addinMenu.label}',className : '${addinMenu.icon}',click : '${addinMenu.url}'};
        // </c:forEach>
        canDel: false,
        _getCtpTop: null,
        dataType: '', // 0：当前数据；1：转储数据
        handoverType: '',
        deduplication: false,
        aiProcessing: false,
        selectedRows: [],
        searchConditionsValue: {}
      };
    },
    computed: {
      tableRequest() {
        const { dataType, handoverType, deduplication, aiProcessing } = this;
        return {
          url: '/ajax.do?method=ajaxAction&managerName=colManager',
          managerMethod: 'getSentList',
          otherArgs: {
            state: '2', //
            type: handoverType,
            deduplication,
            dumpData: dataType === '1' ? 'true' : 'false',
            aiProcessing: aiProcessing,
            templeteIds: urlParamObj.textfield || ''
          }
        };
      },
      toolButtonList() {
        return this.allToolBtns;
      }
    },
    created() {
      this.tableSlotNames = [
        'subject',
        'processDeadLineName',
        'affairArchiveId',
        'isTrack',
        'processId',
        'currentNodesInfo'
      ];
      this.metaDataRequestParams = {
        key: 'LIST_SENT',
        platform: 0
      };
      this.listFrom = 'listSent';
      this.resetTableData = () => {};
    },
    mounted() {
      this.init();
    },
    methods: {
      init() {
        this.getCtpTop();
        this.getResources();
        this.urlParamObj = urlParamObj;
      },
      getCtpTop() {
        this._getCtpTop = window.getCtpTop();
      },
      getResources() {
        this.resources = window._resources;
      },
      metaDataChangeHandle(data) {
        const { globalParam } = data;
        this.initGlobalParam(globalParam);
        this.initToolbar();
      },
      initGlobalParam(globalParam) {
        const keyList = [
          'newColNodePolicy',
          'hasDumpData',
          'showHandoverButton',
          'canDel',
          'haveNewColl'
        ];
        this.hasDoc = window._hasPlugin('doc');
        this.isFormAdvanced = window._hasPlugin('formAdvanced');
        this.emailShow = window._hasPlugin('webmail');
        keyList.forEach((key) => {
          this[key] = globalParam[key];
        });
        this.supportSmartCode = globalParam ? globalParam.supportSmartCode : undefined;
      },
      //按钮点击事件
      handleToolClick(buttonId, { selectedRows, searchConditionsValue }, resetTableData) {
        this.selectedRows = selectedRows;
        this.resetTableData = resetTableData;
        this.searchConditionsValue = searchConditionsValue;
        const messageMap = {
          newColl: $.i18n('collaboration.grid.alert.transmitCol'),
          email: $.i18n('collaboration.grid.alert.transmitCol'),
          pigeonhole: $.i18n('collaboration.pighole.alert.select'),
          cancelWorkFlow: $.i18n('collaboration.listSent.selectRevokeSyn'),
          editWorkFlow: $.i18n('collaboration.sendGrid.selectColEdit'),
          resend: $.i18n('collaboration.grid.send.selectRepeatCol'),
          delete: $.i18n('collaboration.grid.selectDelete'),
          relationAuthority: $.i18n('collaboration.listSent.selectAuthorized')
        };
        if (this.selectedRows.length < 1 && buttonId !== 'smartCodeScan') {
          Fiber.message.warning(messageMap[buttonId]);
          return;
        }
        this[`${buttonId}ClickHandle`](resetTableData);
        document?.activeELement?.blur();
      },
      onRowClick(row) {
        const self = this;
        const { dataType } = this;
        const { affairId } = row;
        if (!isAffairValid(affairId)) {
          callBackendMethod('handoverManager', 'removeAffairById', affairId, {
            success: function () {
              self.resetTableData();
            }
          });
          return;
        }
        var url =
          _ctxPath +
          '/collaboration/collaboration.do?method=summary&openFrom=listSent&affairId=' +
          affairId +
          '&dumpData=' +
          dataType;
        showDetailPage(url, affairId);
      }
    }
  };
</script>
